﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using FFShellAPI.FFMPEG.Settings;
using System.Windows;
using System.ComponentModel;
using MyLib.System.IO;
using System.IO;
using System.Diagnostics;

namespace AVCWizard
{
    [Serializable]
    public class TableColumnConfig
    {
        public TableColumnConfig()
        {
            FileNameWidth = 238;
            AudioFormatWidth = 84;
            VideoFormatWidth = 84;
            BitrateWidth = 64;
            ResolutionWidth = 64;
            LengthWidth = 64;
        }

        public double FileNameWidth { get; set; }
        public double AudioFormatWidth { get; set; }
        public double VideoFormatWidth { get; set; }
        public double BitrateWidth { get; set; }
        public double ResolutionWidth { get; set; }
        public double LengthWidth { get; set; }
    }

    [Serializable]
    public class WindowConfigClass
    {
        public WindowConfigClass()
        {
            State = WindowState.Normal;
            Height = 481;
            Width = 925;
        }

        double _width;
        public double Width
        {
            get { return _width; }
            set 
            {
                if (State == WindowState.Normal)
                    _width = value; 
            }
        }

        double _height;
        public double Height
        {
            get { return _height; }
            set 
            {
                if (State == WindowState.Normal)
                    _height = value; 
            }
        }

        WindowState _state;
        public WindowState State 
        {
            get { return _state; }
            set { _state = value; }
        }
    }

    [Serializable]
    public class AppConfig : ICloneable, INotifyPropertyChanged
    {
        string _FFMpegPath;
        public string FFMpegPath
        {
            get { return _FFMpegPath; }
            set 
            { 
                _FFMpegPath = value;
                OnPropertyChanged("FFMpegPath");
            }
        }

        string _TargetFolder;
        public string TargetFolder
        {
            get { return _TargetFolder; }
            set 
            { 
                _TargetFolder = value;
                OnPropertyChanged("TargetFolder");
            }
        }

        int _MaxAudioBitrate;
        public int MaxAudioBitrate
        {
            get { return _MaxAudioBitrate; }
            set 
            { 
                _MaxAudioBitrate = value;
                OnPropertyChanged("MaxAudioBitrate");
            }
        }

        int _MaxVideoBitrate;
        public int MaxVideoBitrate
        {
            get { return _MaxVideoBitrate; }
            set 
            { 
                _MaxVideoBitrate = value;
                OnPropertyChanged("MaxVideoBitrate");
            }
        }

        int _selectedLangId;
        public int SelectedLangId
        {
            get { return _selectedLangId; }
            set 
            { 
                _selectedLangId = value;
                OnPropertyChanged("SelectedLangId");
            }
        }

        public string LanguageId { get; set; }

        public AppConfig()
        {
            TargetFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonVideos);
            FFMpegPath = @"C:\ffmpeg";
            SelectedLangId = 1;
            LanguageId = "en-US";
            MaxAudioBitrate = 320;
            MaxVideoBitrate = 9000;
            LoadDefaults();
        }

        private void LoadDefaults()
        {
            try
            {
                using (Stream ini = File.OpenRead(StaticExt.AssemblyPath("default.ini")))
                {
                    PropertyLoader prop = new PropertyLoader(ini);
                    prop.GetPropertys(this, "settings");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }
        }

        public object Clone()
        {
            return MemberwiseClone();
        }

        // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    [Serializable]
    public class AVCConfig : IConfigNotifier
    {
        public AppConfig AppCfg { get; set; }
        public WindowConfigClass WindowConfig { get; set; }
        public TableColumnConfig TableConfig { get; set; }
        public AVCodec[] Codecs { get; set; }
        public ObservableCollection<MediaListItem> MediaList { get; set; }
        public bool IsAudioExpanded { get; set; }
        public bool IsVideoExpanded { get; set; }
        public bool IsBaseExpanded { get; set; }

        public AVCConfig()
        {
            WindowConfig = new WindowConfigClass();
            TableConfig = new TableColumnConfig();
            MediaList = new ObservableCollection<MediaListItem>();
            AppCfg = new AppConfig();
            IsBaseExpanded = true;
            IsAudioExpanded = false;
            IsVideoExpanded = false;
        }

        public void BeforeSave()
        {
            if (WindowConfig.State == WindowState.Minimized)
                WindowConfig.State = WindowState.Normal;
        }

        public void AfterLoad()
        {
        }
    }
}
